<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User extends MY_Controller {
	public function __construct(){

		// $this->need_login=true;
		parent::__construct();

	}
	public function adduser()
	{
	$this->load->view('admin/user.html');
	}

	public function useradd(){
		$this->load->view('admin/useradd.html');
	}

	public function changepwd(){
		$this->load->database();

		if (!empty($_POST)) {
			$username = $_COOKIE['mercode'];
			$newpass = md5($_POST['newpass']);
			$newkey = $_POST['newkey'];
			if($newkey!=''&& strlen($newkey)==32){
				$sql = "UPDATE file_user SET password = '$newpass',key = '$newkey' WHERE username='$username'"; 	  		
	  	  		$query = $this->db->query($sql);
	  	  		$row = $query->row();
	  	  		if($row!=''){
	  	  			echo json_encode(array('code'=>'0','msg'=>'修改成功'));
	  	  			exit;
	  	  		}else{
	  	  			echo json_encode(array('code'=>'30006','msg'=>'密钥有问题'));
	  	  			exit;
	  	  		}
			}else{
				$sql = "UPDATE file_user SET password = '$newpass' WHERE username='$username'"; 
				
	  	  		$query = $this->db->query($sql);
	  	  		
	  	  		if($query==true){
	  	  			echo json_encode(array('code'=>'0','msg'=>'修改成功'));
	  	  			exit;
	  	  		}else{
	  	  			echo json_encode(array('code'=>'30006','msg'=>'修改失败'));
	  	  			exit;
	  	  		}
			}

			
		}
		$this->load->view('admin/changepwd.html');
	}
	public function login(){
		$this->load->helper('func');
		$this->load->helper('captcha');
		$this->load->database();


		if(!empty($_POST)){
			
			$username = $_POST['username'];
			$password = md5($_POST['password']);
			$sql = "SELECT * FROM file_user WHERE  username = '$username' and password='$password' ";
			
			 $query = $this->db->query($sql);
			 $logintime = date('Y-m-d H:i:s');
			 
        	 $row = $query->row();
        	 $sql = "UPDATE file_user SET logintime = '$logintime' WHERE username='$username'"; 
				
	  	  		$res = $this->db->query($sql);	 

        	 if($row->id!=''&&$res=='1'){
        	 	echo json_encode(array('code'=>'0','msg'=>'登录成功'));
        	 	exit;
        	 }else{
        	  echo	json_exit("30005","登录失败");
        	 }
		}
		//验证码
// 		$vals = array(
// 	    'word'      => 'Random word',
// 	    'img_path'  => APPPATH.'/captcha/',
// 	    'img_url'   => 'http://www.tv.com/svn_file_service/file_service/captcha/',
// 	    'font_path' => './path/to/fonts/texb.ttf',
// 	    'img_width' => '150',
// 	    'img_height'    => 30,
// 	    'expiration'    => 7200,
// 	    'word_length'   => 8,
// 	    'font_size' => 16,
// 	    'img_id'    => 'Imageid',
// 	    'pool'      => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',

// 	    // White background and border, black text and red grid
// 	    'colors'    => array(
// 	        'background' => array(255, 255, 255),
// 	        'border' => array(255, 255, 255),
// 	        'text' => array(0, 0, 0),
// 	        'grid' => array(255, 40, 40)
// 	    )
// 	);

// $cap = create_captcha($vals);
// echo $cap['image'];
		

		$this->load->view('admin/login.html');
	}
}